<template>
  <!-- vue实例外创建 -->
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
</el-breadcrumb>
    <el-card class="box-card">
        <el-input placeholder="请输入内容" v-model="page.query" class="input-with-select">
    <el-button slot="append" icon="el-icon-search" @click="getdata(1)"></el-button>
  </el-input>
      <el-button class="add" type="primary" @click="drawer = true"
        >添加</el-button
      >
      <el-table :data="userlist" stripe style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            <span>{{ (scope.row.create_time * 1000) | newdate }}</span>
          </template>
        </el-table-column>
        <el-table-column  width="80" prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色名称"> </el-table-column>
        <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 50]"
        :current-page="page.pagenum"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加窗口 -->
    <el-drawer
      title="添加用户"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form
        :rules="rules"
        ref="userref"
        :model="userform"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userform.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userform.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userform.email"></el-input>
        </el-form-item>
        <el-button class="add" type="primary" @click="adduserfun"
          >确定</el-button
        >
      </el-form>
    </el-drawer>
    <!-- 修改窗口 -->
    <el-dialog
  title="修改用户信息"
  :visible.sync="dialogVisible"
  width="50%"
  :before-close="handleClose">
  <el-form
        :rules="rules"
        ref="userref"
        :model="userform"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userform.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userform.email"></el-input>
        </el-form-item>
      </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="edituserfun">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
//   引入接口
import { GetUsers, AddUsers ,GetOenUsers ,EditUsers ,DelOenUsers} from "@/api/user";
export default {
  data() {
    return {
      page: {
        pagenum: 1,
        pagesize: 5,
        query:'',
      },
    //   控制修改弹窗显示与隐藏
      dialogVisible:false,
      
      //   总条数
      total: 0,
      //用户列表
      userlist: [],
      // 添加区域
      // 控制弹窗开关
      drawer: false,
      direction: "rtl",
      userform: {},
      // 数据检验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      title:''
    };
  },
  watch:{
    drawer(){
        this.userform = {}
    },
    dialogVisible(){
        this.userform = {}
    }
  },
  mounted() {
    this.getdata();
    // this.$bus.$on('getTitle',(e)=>{
    //     console.log(e);
        
    //     this.title = e
    // })

    this.title = localStorage.getItem('title')
  },
  methods: {
    // 分页器
    handleSizeChange(val) {
      this.page.pagesize = val;
      this.page.pagenum = 1;
      this.getdata();
    },
    handleCurrentChange(val) {
      this.page.pagenum = val;
      this.getdata();
    },
    // 添加区域
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 确认添加
    adduserfun() {
      this.$refs["userref"].validate(async (v) => {
        if (v) {
          // 发起添加请求
          let { data, meta } = await AddUsers(this.userform);
          if (meta.status != 201) return this.$message.error(meta.msg);
          this.$message.success(meta.msg);
          // 1.关闭窗口
          this.drawer = false;
          // 2.更新页面
          this.getdata();
          // 3.清空之前的数据
          this.userform = {};
          // this.$refs['userref'].resetFields();
        }
      });
    },
    // 修改
    async handleEdit(index, {id}) {
        this.dialogVisible= true
        console.log(id);
        // 发起请求获取单条记录
        let {data,meta} = await GetOenUsers(id)
        if (meta.status != 200) return this.$message.error(meta.msg);
        this.userform = data
        
      },
    //   确定修改
    async edituserfun(){
        console.log(this.userform);
        let {data,meta} = await EditUsers(this.userform)
        if (meta.status != 200) return this.$message.error(meta.msg);
        this.$message.success(meta.msg);
         // 1.关闭窗口
         this.dialogVisible = false;
          // 2.更新页面
          this.getdata();
          // 3.清空之前的数据
          this.userform = {};

    },
    //   删除
     async handleDelete(index, {id}) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
            let {data,meta}=await DelOenUsers(id)
       if (meta.status != 200) return this.$message.error(meta.msg);
       this.$message.success(meta.msg);
       this.getdata()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
     
      },
    // 获取数据
    async getdata(e) {
        if (e==1) {
            this.page.pagenum=1
        }
      let { data, meta } = await GetUsers(this.page);
      if (meta.status != 200) return this.$message.error(meta.msg);
      this.userlist = data.users;
      this.total = data.total;
    },
  },
};
</script>

<style scoped>
.add {
  margin-bottom: 10px;
}
.el-form {
  text-align: center;
}
.input-with-select{
    width: 30%;
    margin-right: 15px;
}
</style>
